<template>
  <div id="home">
    <NavBar class="home-nav">
      <div slot="center">购物车</div>
    </NavBar>
    <TabControl :titles="['流行','新款','精选']"
                @tabClick="tabClick" ref="tabControl1"
                class="tab-control"
                v-show="isTabFixed"
    ></TabControl>

    <scroll class="content" ref="scroll"
            @scroll="contentScroll"
            :probe-type-num="3"
            :pull-up-load="true"
            @pullingUp="pullingUp"
    >
      <HomeSwiper :banner="banner" @itemLoad="swiperItemLoad"></HomeSwiper>
      <RecommendView :recommend="recommend"></RecommendView>
      <FeatureView></FeatureView>
      <TabControl :titles="['流行','新款','精选']" @tabClick="tabClick" ref="tabControl2"></TabControl>
      <GoodsList :goods="showGoods"></GoodsList>
    </scroll>
    <back-top @click.native="backClick" v-show="backTopShow"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import TabControl from "../../components/content/TabControl";
import GoodsList from "../../components/content/goods/GoodsList";
import Scroll from "../../components/common/scroll/Scroll";

import HomeSwiper from "./childComps/HomeSwiper";
import FeatureView from "./childComps/FeatureView";
import RecommendView from "./childComps/RecommendView";

import {itemListenerMixin,backTop} from "../../common/mixin";
import {getHomeMultiData, getHomeGoods} from "../../network/home";

export default {
  name: "home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,

  },
  mixins:[itemListenerMixin,backTop]
  ,data() {
    return {
      banner: [],
      dKeyword: [],
      keywords: [],
      recommend: [],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []}
      },
      currentType: 'pop',
      tobOffsetTop: 0,
      isTabFixed: false,
      saveY:0
    }
  }, created() {
    //获取数据
    this.getHomeMultiData()
    //获取商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')


  }, mounted() {
  },
  activated() {
    //跳转到记录的位置
      this.$refs.scroll.scrollTo(0,this.saveY,0)
      this.$refs.scroll.refresh()

  },deactivated() {
    //记录跳转之前的位置信息
  this.saveY=this.$refs.scroll.scroll.y;
  this.$bus.$off("ImageLoad",this.itemImgListener)
  }
  ,
methods: {
    /*
    *事件监听的相关方法
    * */


    //监听轮播图是否已经加载完成
    swiperItemLoad() {
      // console.log(this.$refs.tabControl2.$el.offsetTop);
      this.tobOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    //选择是哪个分类
    tabClick(index) {
      // console.log(index)
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    //上滑图标隐藏与显示
    contentScroll(postion) {
      this.backTopShow = (-postion.y) > 1000;
      this.isTabFixed = (-postion.y) > this.tobOffsetTop;
    },
    //监控刷新
    pullingUp() {
      this.getHomeGoods(this.currentType)
    },
    /*
    * 网络请求
    * */
    getHomeMultiData() {
      getHomeMultiData().then((res) => {
        // console.log(res)
        this.banner = res.data.banner.list;
        this.dKeyword = res.data.dKeyword.list;
        this.keywords = res.data.keywords.list;
        this.recommend = res.data.recommend.list
      })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1

        //刷新完成
        //作用：当上拉加载数据加载完毕后，需要调用此方法告诉 better-scroll 数据已加载。
        this.$refs.scroll.finishPullUp();
      })
    }
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    }
  }
}
</script>


<style scoped>
#home {
  /*padding-top: 44px;*/
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;

  /*在使用浏览器原生滚动时, 为了让导航不跟随一起滚动*/
  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 9;*/
}

.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

.tab-control {
  position: relative;
  z-index: 9;
}

/*.content {*/
/*height: calc(100% - 93px);*/
/*overflow: hidden;*/
/*margin-top: 44px;*/
/*}*/
</style>
